//
//  DDCommonDataManager.m
//  DingDongZP
//
//  Created by Jyh on 16/4/26.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDCommonDataManager.h"
#import "DDCommonDataService.h"

#define kDocumentPath @"[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]"

static NSString *eduPath = @"edu.data";
static NSString *salaryPath = @"salary.data";
static NSString *experiencePath = @"experience.data";
static NSString *companySizePath = @"companySize.data";
static NSString *provincePath = @"province.data";


/**
 *  取公共数据的类
 */
@implementation DDCommonDataManager


+ (instancetype)shareManager
{
    static id sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

- (void)getEduArrayWithBlock:(void(^) (NSArray *eduArray))resultArray
{
    NSArray *edusArray = [@[] copy];
    
    NSString *path = [self makeDocumentPath:eduPath];
    
    edusArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (!edusArray) {
        //请求数据
        DDCommonDataService *service = [[DDCommonDataService alloc] init];
       
        [service  queryAllEduInfo:^(NSArray *edus) {
            if ([edus count] > 0) {
                // 归档存入
                [NSKeyedArchiver archiveRootObject:edus toFile:path];
            }
            
            resultArray(edus);
        }];
    } else {
        
        resultArray(edusArray);
    }
}

- (void)getSalaryArrayWithBlock:(void(^) (NSArray *salaryArray))resultArray
{
    NSArray *salariesArray = [@[] copy];
    
    NSString *path = [self makeDocumentPath:salaryPath];
    
    salariesArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (!salariesArray) {
        //请求数据
        DDCommonDataService *service = [[DDCommonDataService alloc] init];
        
        [service queryAllSalariesInfo:^(NSArray *salaries) {
            if ([salaries count] > 0) {
                // 归档存入
                [NSKeyedArchiver archiveRootObject:salaries toFile:path];
            }
            
            resultArray(salaries);
        }];
    } else {
        resultArray(salariesArray);
    }
}

- (void)getExperienceArrayWithBlock:(void(^) (NSArray *experienceArray))resultArray
{
    NSArray *experiencesArray = [@[] copy];
    
    NSString *path = [self makeDocumentPath:experiencePath];
    
    experiencesArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (!experiencesArray) {
        //请求数据
        DDCommonDataService *service = [[DDCommonDataService alloc] init];
        
        [service queryAllExperienceInfo:^(NSArray *experiences) {
            if ([experiences count] > 0) {
                // 归档存入
                [NSKeyedArchiver archiveRootObject:experiences toFile:path];
            }
            
            resultArray(experiences);
        }];
    } else {
        resultArray(experiencesArray);
    }
}

- (void)getCompanySizeArrayWithBlock:(void(^) (NSArray *sizeArray))resultArray
{

    NSArray *companySizeArray = [@[] copy];
    
    NSString *path = [self makeDocumentPath:companySizePath];
    
    companySizeArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (!companySizeArray) {
        //请求数据
        DDCommonDataService *service = [[DDCommonDataService alloc] init];
        
        [service queryAllCompanySize:^(NSArray *companysizes) {
            if ([companysizes count] > 0) {
                // 归档存入
                [NSKeyedArchiver archiveRootObject:companysizes toFile:path];
            }
            
            resultArray(companysizes);
        }];
    } else {
        resultArray(companySizeArray);
    }
}

- (void)getProvincesArrayWithBlock:(void(^) (NSArray *provinceArray))resultArray
{
    NSArray *provinceArray = [@[] copy];
    
    NSString *path = [self makeDocumentPath:provincePath];
    
    provinceArray = (NSArray *)[NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
//    if (provinceArray.count > 0) {
//        
//        resultArray(provinceArray);
//        
//    } else {
    
        //请求数据
        DDCommonDataService *service = [[DDCommonDataService alloc] init];
        
        [service queryAllProviceInfo:^(NSArray *provices) {
            if ([provices count] > 0) {
                // 归档存入
                [NSKeyedArchiver archiveRootObject:provices toFile:path];
            }
            
            resultArray(provices);
        }];
//    }

    
}

-(NSString *)makeDocumentPath:(NSString*)fullNamespace
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths[0] stringByAppendingPathComponent:fullNamespace];
}

@end
